﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace FunstreamTV.Tools
{
    public static class FileManager
    {
        public static async Task Save<T>(T data, string fileName)
        {
            var json = Serializer.Serialize(data);
            var file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
            await FileIO.WriteBytesAsync(file, Encoding.UTF8.GetBytes(json));
        }

        public static async Task<T> Load<T>(string fileName)
        {
            var file = await ApplicationData.Current.TemporaryFolder.GetFileAsync(fileName);
            var buffer = await FileIO.ReadBufferAsync(file);
            var model = Serializer.Deserialize<T>(Encoding.UTF8.GetString(buffer.ToArray(), 0, (int)buffer.Length));
            return model;
        }

        public static async Task Remove(string fileName)
        {
            var file = await ApplicationData.Current.TemporaryFolder.GetFileAsync(fileName);
            await file.DeleteAsync();
        }
    }
}
